const codeidea = require("../models/codeidea")

var express = require('express');
var router = express.Router();

const formidable = require('formidable');

//返回图片
router.post('/', (req, res, next) => {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
        if (err) {
            res.json({ 'state': false })
        } else {
          codeidea.find({},{
              process: 1
            }, async (error, doc) => {
                if (error) {
                    res.json({ 'state': false, 'msg': error });
                } else {
                    let count = 0;
                    console.log(doc.length)
                    for(let i=0;i<doc.length;i++){
                        // console.log(doc[i].process[0].html);
                        if(doc[i].process[0].html==undefined){
                            // console.log(doc[i].process.length);
                            // count++;
                            let update = doc[i].process[0];
                            update['html'] = "";
                            update['css'] = "";
                            update['js'] = "";
                            let new_process = [];
                            new_process.push(update);
                            // console.log(update);
                            await codeidea.updateOne({_id: doc[i]._id}, {
                                    $set:{
                                        process: new_process
                                    }
                                }, (err, data) => {
                                    if(err) {
                                        res.json({state: false, msg: err})
                                    }else {
                                        count++;
                                    }
                                })
                        }
                    }
                    res.json({ 'state': true, 'msg': count });
                }
            })

        }

    });

});

module.exports = router;